/***************** Modification History ***********************************
   @doc
   @module report2.c - Application Report functions |
   @end
   
$Log: /SoftPay/SPK2SIM/Source/Main/report2.c $ 
**************************************************************************

   Copyright (C) 1999 by VeriFone, Inc.

   All rights reserved.  No part of this software may be reproduced,
   transmitted, transcribed, stored in a retrieval system, or translated
   into any language or computer language, in any form or by any means,
   electronic, mechanical, magnetic, optical, chemical, manual or otherwise,
   without the prior written permission of VeriFone, Inc.

---------------------------------------------------------------------------*/
#include <project.h>
#define VSREPT2_C

#include <aclconio.h>
#include <stdlib.h>
#include <stdio.h>
#include <applidl.h>
#include <svctxo.h>
#include <aclstr.h>
#include <dbmgr.h>
#include <formater.h>
#include <string.h>

/** /#define DEBUG_VSREPORT*/
#ifdef DEBUG_VSREPORT
#define DEBUG_95
#endif

#include <define.h>
#include <transact.h>
#include <report.h>
#include <bdt.h>
#include <cdt.h>
#include <cst.h>
#include <hdt.h>
#include <tct.h>
#include <lct.h>
#include <tblutil.h>
#include <pct.h>
#include <sdt.h>
#include <msg.h>
#include <mit.h>
#include <msgfuncs.h>
#include <print.h>
#include <tran.h>
#include <ui.h>
#include <form.h>
#include <form2.h>
#include <accum.h>
#include <date.h>
#include <spdebug.h>
#include <codesec.h>
#include <mdlfuncs.h>
#include <pbatch.h>
#include <format.h>

extern char* pchGetMessage(long);

/*-------------------------------------------------------------------------
    Function :    vdSetHostCaptureReportCondition()
    Description : Sets the conditions for formatter.
    Parameters :
        fCreditHost - boolean value true/false whether host supports host
        captured credit transaction data.
        fDebitHost - boolean value true/false whether host supports host
        captured debit transaction data.
    Returns : none.
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      0   04/30/97 Matt_V1    Create
--------------------------------------------------------------------------*/

void vdSetHostCaptureReportCondition(TRANSACTION_OBJECT* pobTran,VS_BOOL fCreditHost,VS_BOOL fDebitHost) {
    lnMiddleCondition = formater_set_flags(0, (fGetDemoMode()), (inGGCode() == BAL_INQ), (inGGCode() == ONLINE_BATCH_CLEAR), 0, ((fCreditHost == VS_TRUE) && (fDebitHost == VS_TRUE)), (fCreditHost == VS_TRUE), (fDebitHost == VS_TRUE), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1);
}


/*-------------------------------------------------------------------------
    Function :    inPrintHostCaptureReport
    Description : Prints the Host Capture Report (Batch Inquiry or Clear Batch)
        report dependent on the transaction code.  For each report, prints a
        credit section if the data capture host supports credit, and a debit
        section if the data capture host supports debit.  A grand totals line
        is printed when both of the aforementioned sections are printed.
    Parameters :
        fCreditHost - boolean value true/false whether host supports host
        captured credit transaction data.
        fDebitHost - boolean value true/false whether host supports host
        captured debit transaction data.
        fCommErr
        obPrinter
    Returns :     VS_ERR on error and VS_SUCCESS on success
    Globals :     srGDS.inCode
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      0   04/30/97 Matt_V1    Created.
      1   06/04/97 Matt_V1    Added parameter in call to
                              vdSetPrintHeaderCondition.
      2   02/16/98 Page_K1    Add param(receipt/report) to inPrintReceiptHeader() call
      3   09/15/98 Page_K1    Use inGetReport2Template() instead of "rept"                            
--------------------------------------------------------------------------*/

int inPrintHostCaptureReport(VS_BOOL fCreditHost,VS_BOOL fDebitHost,VS_BOOL fCommErr,PRINTER_OBJECT* obPrinter,TRANSACTION_OBJECT* pobTran) {
    if (inGetPrinterType() == NOPRINTER && fGetEnablePrinter()) {
        vdDisplayMessageFromFile(NO_PRINTER_MSG, WARNING_MSG);
        return(VS_SUCCESS);
    }

    if (!fGetEnablePrinter())
        return(VS_SUCCESS);

    vdDisplayMessageFromFile(PRINTING_MSG, STATUS_MSG);

    inGetReport2Template();

    vdMakeReportHeader(pobTran);

    vdSetPrintHeaderCondition(pobTran, VS_FALSE, VS_FALSE);   /*  VS_FALSE sets header to black print */
    vdSetHostCaptureReportCondition(pobTran, fCreditHost, fDebitHost);
    if (fCommErr)
        lnMiddleCondition |= 0x00400000;

    strlcpy(srRcpt.szBatchNum, pobTran->szBatchNum, BATCH_NUM_SIZE);
    vdLoadDateTime(pobTran);
    inPrintReceiptHeader(pobTran);
    if (inPrintLines(obPrinter, ALL_HOST_CAPTURE_START, ALL_HOST_CAPTURE_END, NO_BL_LINE, lnMiddleCondition) != VS_SUCCESS)
        return(VS_ERR);

    return(VS_SUCCESS);
}


/*-------------------------------------------------------------------------
   Function :    inPrintReptHeader
    Description :
    Parameters :
    Returns :
    Globals :
    Notes :
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      1   02/06/98 Betty_B1    Create for use with REPT*.TPL files
      2   02/16/98 Page_K1    Add param(receipt/report) to inPrintReceiptHeader() call

--------------------------------------------------------------------------*/

int inPrintReptHeader(TRANSACTION_OBJECT* pobTran,PRINTER_OBJECT* obPrinter) {
    /** /pdebug(("--vdMakeReptHeader--"));*/

    inPrintReceiptHeader(pobTran);

    if (inPrintLines(obPrinter, REPT_HEADER_START, REPT_HEADER_END, NO_BL_LINE, lnHeaderCondition) != VS_SUCCESS)
        return(VS_ERR);

    return(VS_SUCCESS);
}


/********************************************************************************************* 
* @func int | inUnadjustedOperation | 
* Initiates unadjusted tip operation
* @parm TRANSACTION_OBJECT *    | pobTran   | 
*
* @rdesc Return values.
* @flag int    |  VS_SUCCESS ,VS_ESCAPE, VS_ERR        
* @end
**********************************************************************************************/

int inUnadjustedOperation(TRANSACTION_OBJECT* pobTran) {
    /*    pdebug(("--inUnadjustedOperation--"));*/

    if (inGetPrinterType() == NOPRINTER && fGetEnablePrinter()) {
        vdDisplayMessageFromFile(NO_PRINTER_MSG, WARNING_MSG);
        return(VS_SUCCESS);
    }
    if (!fGetEnablePrinter())
        return(VS_SUCCESS);

    if (inGetTipProcessing() > TIP_OFF) {
        inGetReport2Template();

        return(inPrintUnadjReport(&obPrinter, pobTran));
    }

    vdDisplayMessageFromFile(TRANS_NOT_ALLWD_ERR_MSG, ERROR_MSG);

    return(VS_ERR);
}



/********************************************************************************************* 
* @func int | inPrintUnadjReport | 
*       Browse through the batch and prints all unadjusted tip trans.
*
* @parm PRINTER_OBJECT *    | obPrinter |
* @parm TRANSACTION_OBJECT *    | pobTran   |
*
* @rdesc Return values.
* @flag int    |  VS_SUCCESS, VS_ESCAPE, VS_ERR        
* @end
**********************************************************************************************/

int inPrintUnadjReport(PRINTER_OBJECT* obPrinter,TRANSACTION_OBJECT* pobTran) {
#ifdef BATCH_DEFINED

    int inReportTop = VS_TRUE;
    int inResult,
    inCnt = 0;
    int inCount = 0;
    int inPrintDetail = VS_FALSE;
    int inStart,
    inStop,
    inMerch = 0;
    int inNewClerk;
    int inNewMerchant; 
    int inTransPrinted = 0 ;

    /** /pdebug(("--inPrintUnadjReport--"));*/
    inStart = 1;
    inStop = inGetMITTotRecs();


    if (inLoadMITRec(0) != VS_SUCCESS)
        return(VS_ERR);


    srRcpt.unFrm.srDet.srMid.inTotalTipCount = 0;
    srRcpt.unFrm.srDet.srMid.lnTotalTipAmt = 0;
    inNewClerk = VS_TRUE;
    inPrintDetail = VS_FALSE;
    for (inMerch = inStart; inMerch <= inStop; inMerch++) {
        if (inLoadMITRec(inMerch - 1) != VS_SUCCESS)
            return(VS_ERR);
        inNewMerchant = VS_TRUE;
        strlcpy(srRcpt.szMerchantName, szGetMerchantText(), MESSAGE_SIZE);

        if (inGetDCBatchRecCnt(pobTran) == 0)
            continue; 

        /* Need this for inGetDCKeyBatchRec() below */
        vdBatchDCOpen(pobTran);

        /* Print online records */
        inResult = inGetDCKeyBatchRec(pobTran, KEY_ALL_RECS, &pobTran->srBKey, DB_FIRST);
        if ((inResult != VS_ERROR) && (inResult != VS_EOB))
            do {
                if (inGGCode() != AUTH_ONLY && inGGCode() != IMAM_CARDVER) {
                    if (inGetTipProcessing() > TIP_OFF && pobTran->srBRec.lnTipAmount == 0L && pobTran->srBRec.inNumberOfTipAdjusts == 0 && !pobTran->srBKey.fVoided && (pobTran->srBRec.inTranCardType != DEBIT_CARD && pobTran->srBRec.inTranCardType != EBT_CARD) && (inResult != VS_EOB)) {
                        if (inNewClerk) {
                            if (inReportTop == VS_TRUE) {
                                vdMakeReportHeader(pobTran);
                                if (inGetMITTotRecs() == 1)
                                    inPrintReptHeader(pobTran, obPrinter);
                            }
                            /* Header condition should not print report title except at top of report */
                            vdSetUnadjustHeaderCondition(inReportTop, inNewClerk);
                            inNewClerk = VS_FALSE;
                            inNewMerchant = VS_FALSE;
                            inReportTop = VS_FALSE;
                            if (inPrintLines(obPrinter, UNADJ_TOP_START, UNADJ_TOP_END, NO_BL_LINE, lnHeaderCondition) != VS_SUCCESS)
                                return(VS_ERR);
                            inTransPrinted++;
                        } else {
                            /* print merchant name if more than one merchant */
                            if (inNewMerchant) {
                                vdSetUnadjustHeaderCondition(inReportTop, inNewClerk);
                                inNewMerchant = VS_FALSE;
                                if (inPrintLines(obPrinter, UNADJ_MERCH_START, UNADJ_MERCH_END, NO_BL_LINE, lnHeaderCondition) != VS_SUCCESS)
                                    return(VS_ERR);
                            }
                        }
                        inCount ++;
                        inPrintDetail = VS_TRUE;
                        vdMakeUnadjMiddle(pobTran);
                        if (inPrintLines(obPrinter, UNADJ_MIDDLE_START, UNADJ_MIDDLE_END, NO_BL_LINE, lnMiddleCondition) != VS_SUCCESS)
                            return(VS_ERR);
                    }
                    if (gu_clr_state())
                        return(VS_ESCAPE);
                }                            
                inResult = inGetDCKeyBatchRec(pobTran, KEY_ALL_RECS, &pobTran->srBKey, DB_NEXT);
            } while ((inResult != VS_ERROR) && (inResult != VS_EOB)); /* end of key */


        vdCloseDCBatch(pobTran);
    } /* end of Merch loop */

    /* Bottom */
    /* print 'no trans' for this clerk */
    inCount = 0;
    if (!inPrintDetail && !inNewClerk) {
        vdSetUnadjustBottomCondition(inCount);
        if (inPrintLines(obPrinter, UNADJ_BOTTOM_START, UNADJ_BOTTOM_END, NO_BL_LINE, lnBottomCondition) != VS_SUCCESS)
            return(VS_ERR);
    }
    if (act_kbd_pending_test(KEY_CANCEL))
        return(VS_ESCAPE);                                   


    if (inTransPrinted > 0) {
        /*  Grand Totals  */
        inCount = 99;
        vdSetUnadjustBottomCondition(inCount);
        if (inPrintLines(obPrinter, UNADJ_BOTTOM_START, UNADJ_BOTTOM_END, NO_BL_LINE, lnBottomCondition) != VS_SUCCESS)
            return(VS_ERR);
    } else {
        vdDisplayMessageFromFile(NO_DATA_MSG, WARNING_MSG);
        SVC_WAIT(1000);
    }



    return (VS_SUCCESS);

#endif /* ifdef BATCH_DEFINED */
} /* end of inPrintUnadjReport() */


/********************************************************************************************* 
* @func void | vdSetUnadjustHeaderCondition | 
* Set conditons for printing the header of the unadjused tip report
* @parm int     | inReportTop   |
* @parm int     | inReportType  |
* @parm int     | inNewClerk    |
*
* @rdesc VOID
* @end
**********************************************************************************************/

void vdSetUnadjustHeaderCondition(int inReportTop,int inNewClerk) {
    if (inGetPrinterType() == NOPRINTER || !fGetEnablePrinter())
        return;

    lnHeaderCondition = formater_set_flags(0, 0, (inReportTop), (inGetMITTotRecs() > 1),

                        (inGetApplicationType() == RESTAURANT && inNewClerk),
                        (inGetApplicationType() == RETAIL && inNewClerk),
                        0,
                        0,
    
                        (inReportTop && fGetDemoMode() == VS_TRUE),
                        0,
                        0,
                        0,

                        0,
                        0,
                        0,
                        0,

                        0,
                        0,
                        0,
                        0,

                        0,
                        0,
                        0,
                        0,

                        0,
                        0,
                        0,
                        0,

                        0,
                        0,
                        0,
                        0, -1);
}



/********************************************************************************************* 
* @func void | vdSetUnadjustBottomCondition | 
* Set conditons for printing the bottom of the unadjused tip report
* @parm int     | inCount   |
*
* @rdesc VOID
* @end
**********************************************************************************************/

void vdSetUnadjustBottomCondition(int inCount) {
    /** /pdebug(("--vdSetUnadjustBottomCondition--"));*/

    if (inGetPrinterType() == NOPRINTER || !fGetEnablePrinter())
        return;

    lnBottomCondition = formater_set_flags((inCount == 0), (inCount > 0),
                        0,
                        0,

                        0,
                        0,
                        0,
                        0,

                        0,
                        0,
                        0,
                        0,

                        0,
                        0,
                        0,
                        0,

                        0,
                        0,
                        0,
                        0,


                        0,
                        0,
                        0,
                        0,

                        0,
                        0,
                        0,
                        0,

                        0,
                        0,
                        0,
                        0, -1);
}




/********************************************************************************************* 
* @func void | vdMakeUnadjMiddle | 
* Set conditons for printing the middle of the unadjused tip report
* @parm TRANSACTION_OBJECT *    | pobTran   | 
*
* @rdesc VOID
* @end
**********************************************************************************************/

void vdMakeUnadjMiddle(TRANSACTION_OBJECT* pobTran) {
    char acctnum[PAN_SIZE + 1];
    int len = 0;

    memset(srRcpt.unFrm.srOTab.srMid.szCType, 0x00, sizeof(srRcpt.unFrm.srOTab.srMid.szCType));

    sprintf(srRcpt.unFrm.srOTab.srMid.szInvNum, "%06ld", pobTran->srBKey.lnInvNum);

    strcpy(srRcpt.unFrm.srOTab.srMid.szCType, szGetCardAbbrev());

    strlcpy(srRcpt.unFrm.srOTab.srMid.szClerkId, pobTran->srBKey.szClerkId, CLERK_ID_SIZE);

    memset(acctnum, 0x00, sizeof(acctnum));

    strcpy(acctnum, pobTran->srBRec.szPAN);
    len = strlen(acctnum);
    strncpy(&srRcpt.unFrm.srOTab.srMid.szAcctNum[0], &acctnum[len - LAST4_DIGIT_ACCT], (SML_ACCT_SIZE + 1));

    sprintf(srRcpt.szAmt, SUBSTITUTE_LD, pobTran->srBKey.lnTotalOfTransactionAmount - pobTran->srBRec.lnTipAmount);

    if (pobTran->srBKey.fVoided)
        strcpy(srRcpt.szAmt, "000");

    vdFormatAmount(srRcpt.szAmt, VS_FALSE);
    vdTrimLeadSpaces(srRcpt.szAmt);
}



int REPORT2_CODE_CHECK(char* a,char* b,short* c,char* d,unsigned char* e) {
    GET_VER(a, b, c, d, e);
    return 1;
}



#undef VSREPT2_C

